﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

namespace Pad
{
    public partial class Form1 : Form
    {
        Pad P = new Pad();
        List<Note> P_ = new List<Note>();
        public Form1()
        {
            InitializeComponent();

            Load += new EventHandler(_DeSerialize);
            
            

        }

        void _DeSerialize(object sender, EventArgs e)
        {
            Stream fStream = File.OpenRead("S.dat");
            P = (Pad)(new BinaryFormatter().Deserialize(fStream));
            fStream.Close();
            Display(P);
        }

        void _Serialize(object sender, FormClosingEventArgs e)
        {
            BinaryFormatter bFormat = new BinaryFormatter();
            Stream fStream = new FileStream("S.dat", FileMode.Create, FileAccess.Write, FileShare.None);
            bFormat.Serialize(fStream, P);
            fStream.Close();
        }

        void Display(Pad p)
        {
            ListView.Items.Clear();
            foreach (Note n in p.PAD)
                ListView.Items.Add(new ListViewItem(new string[] { n.LastName, n.FirstName, n.BirthDay.ToString(), n.Phone }));
        }

        private void поИмениToolStripMenuItem_Click(object sender, EventArgs e)
        {
            P.PAD.Sort(Note.SortByName);
            Display(P);
        }

        private void поФамилииToolStripMenuItem_Click(object sender, EventArgs e)
        {
            P.PAD.Sort(Note.SortByFam);
            Display(P);
        }

        private void поДатеToolStripMenuItem_Click(object sender, EventArgs e)
        {
            P.PAD.Sort(Note.SortByDR);
            Display(P);
        }

        private void удалитьToolStripMenuItem_Click(object sender, EventArgs e)
        {
            P.PAD.RemoveAt(P.PAD.Count - 1);
            Display(P);
        }

        private void добавитьToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new_Note newN = new new_Note();
            if (newN.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    if (newN.Fam_ != "" && newN.Name_ != "" && newN.Phone_ != "")
                    {
                        P.Add(new Note(newN.Fam_, newN.Name_, newN.Phone_, int.Parse(newN.DR_)));
                        Display(P);
                    }
                    else
                        MessageBox.Show("Некорректные данные.");
                }
                catch { MessageBox.Show("Некорректные данные."); }
                newN.Dispose();
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

            Display(new Pad(P.DSearchName(firstName: textBox1.Text, lastName: textBox2.Text).ToArray()));

        }

        


    }
}
